/*
 * This file is part of jboss-admin-rest
 * Copyright 2009, Christos Vasilakis (cvasilak@gmail.com)
 * 
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.admin.rest.domain;

import org.jboss.admin.rest.util.ManagedComponentUtils;
import org.jboss.managed.api.ManagedComponent;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@XmlRootElement(name = "server")
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(propOrder = {"version", "serverName", "homeDir", "serverHomeDir", "serverLogDir", "startDate", 
					  "hostName", "hostAddress", "OSArch", "OSVersion", "javaVMName", "javaVendor",
					  "javaVersion", "javaVMVersion", "totalMemory", "maxMemory", "freeMemory", "activeThreadCount", 
					  "activeThreadGroupCount", "availableProcessors", "numberOfTransactions",  "numberOfCommittedTransactions",
					  "systemProperties"
					 })
public class ServerInfoBean {
	
	private ManagedComponent serverInfo;
	private ManagedComponent server;
	private ManagedComponent serverConfig;
	private ManagedComponent jta;
	
	private final static SystemPropertiesContainer systemProperties = new SystemPropertiesContainer(System.getProperties());
	
	public ServerInfoBean() {
	}
	
	public ServerInfoBean(ManagedComponent serverInfo, ManagedComponent server,
								 ManagedComponent serverConfig, ManagedComponent jta) {
		this.serverInfo = serverInfo;
		this.server = server;
		this.serverConfig = serverConfig;
		this.jta = jta;
	}
	
	@XmlElement(name = "version")
	public String getVersion() {
		return "JBoss" + " [" + ManagedComponentUtils.getProperty("versionName", server) + "] " + ManagedComponentUtils.getProperty("version", server);
	}

	@XmlElement(name = "serverName")
	public String getServerName() {
		return ManagedComponentUtils.getProperty("serverName", serverConfig);
	}

	@XmlElement(name = "homeDir")
	public String getHomeDir() {
		return ManagedComponentUtils.getProperty("homeDir", serverConfig);
	}
	
	@XmlElement(name = "serverHomeDir")
	public String getServerHomeDir() {
		return ManagedComponentUtils.getProperty("serverHomeDir", serverConfig);
	}
	
	@XmlElement(name = "serverLogDir")
	public String getServerLogDir() {
		return ManagedComponentUtils.getProperty("serverLogDir", serverConfig);
	}

	@XmlElement(name = "startDate")
	public String getStartDate() {
		return ManagedComponentUtils.getProperty("startDate", server);
	}

	@XmlElement(name = "hostName")
	public String getHostName() {
		return ManagedComponentUtils.getProperty("hostName", serverInfo);
	}
	
	@XmlElement(name = "hostAddress")
	public String getHostAddress() {
		return ManagedComponentUtils.getProperty("hostAddress", serverInfo);
	}
	
	@XmlElement(name = "OSArch")
	public String getOSArch() {
		return ManagedComponentUtils.getProperty("OSArch", serverInfo);
	}
	
	@XmlElement(name = "OSVersion")
	public String getOSVersion() {
		return ManagedComponentUtils.getProperty("OSVersion", serverInfo);
	}
	
	@XmlElement(name = "javaVMName")
	public String getJavaVMName() {
		return ManagedComponentUtils.getProperty("javaVMName", serverInfo);
	}
	
	@XmlElement(name = "javaVendor")
	public String getJavaVendor() {
		return ManagedComponentUtils.getProperty("javaVendor", serverInfo);
	}
	
	@XmlElement(name = "javaVersion")
	public String getJavaVersion() {
		return ManagedComponentUtils.getProperty("javaVersion", serverInfo);
	}
	
	@XmlElement(name = "javaVMVersion")
	public String getJavaVMVersion() {
		return ManagedComponentUtils.getProperty("javaVMVersion", serverInfo);
	}
	
	@XmlElement(name = "totalMemory")
	public String getTotalMemory() {
		return ManagedComponentUtils.getProperty("totalMemory", serverInfo);
	}
	
	@XmlElement(name = "maxMemory")
	public String getMaxMemory() {
		return ManagedComponentUtils.getProperty("maxMemory", serverInfo);
	}
	
	@XmlElement(name = "freeMemory")
	public String getFreeMemory() {
		return ManagedComponentUtils.getProperty("freeMemory", serverInfo);
	}
	
	@XmlElement(name = "activeThreadCount")
	public String getActiveThreadCount() {
		return ManagedComponentUtils.getProperty("activeThreadCount", serverInfo);
	}

	@XmlElement(name = "activeThreadGroupCount")
	public String getActiveThreadGroupCount() {
		return ManagedComponentUtils.getProperty("activeThreadGroupCount", serverInfo);
	}
	
	@XmlElement(name = "availableProcessors")
	public String getAvailableProcessors() {
		return ManagedComponentUtils.getProperty("availableProcessors", serverInfo);
	}
	
	@XmlElement(name = "numberOfTransactions")
	public String getNumberOfTransactions() {
		return ManagedComponentUtils.getProperty("numberOfTransactions", jta);
	}
	
	@XmlElement(name = "numberOfCommittedTransactions")
	public String getNumberOfCommittedTransactions() {
		return ManagedComponentUtils.getProperty("numberOfCommittedTransactions", jta);
	}
	
	@XmlElement(name = "systemProperties")
	public SystemPropertiesContainer getSystemProperties() {
		return systemProperties;
	}
	
	@XmlAccessorType(XmlAccessType.NONE)
	static class SystemPropertiesContainer {
		
		private List<SystemProperty> properties;
		
		SystemPropertiesContainer() {
		}
		
		SystemPropertiesContainer(Properties p) {
			properties = new ArrayList<SystemProperty>();
			
			for (Map.Entry<Object, Object> property: p.entrySet()) {
				properties.add(new SystemProperty( (String)property.getKey(), (String)property.getValue() ));
			}
		}
		
		@XmlElement(name = "property")
		public List<SystemProperty> getProperties() {
			return properties;
		}
	}
	
	@XmlAccessorType(XmlAccessType.NONE)
	@XmlType(propOrder = {"key", "value"})
	static class SystemProperty {
		
		private String key;
		private String value;
		
		SystemProperty() {
		}
		
		SystemProperty(String key, String value) {
			this.key = key;
			this.value = value;
		}
		
		@XmlElement(name = "key")
		public String getKey() {
			return key;
		}
		
		@XmlElement(name = "value")
		public String getValue() {
			return value;
		}
	}
}
